package com.java.loginsso.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.java.base.service.RedisService;
import com.java.loginsso.mapper.UserMapper;
import com.java.loginsso.pojo.User;
import com.java.loginsso.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @ClassName UserServiceImpl
 * @Description 用户service实现
 * @Author Trigger Wu
 * @Date 2018/9/1910:37
 * @Version 1.0
 **/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    @Autowired
    private RedisService redisService;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    private static final Integer REDIS_TIME = 60 * 30;
    /*
     * 检查用户注册类型
     *
     * @author Tigger Wu
     * @date 2018/9/19 11:06
     * @param 
     * @return 
     */
    public Boolean check(String param, Integer type) {
        User record = new User();
        EntityWrapper<User>wrapper = new EntityWrapper<>();
        switch (type) {
            case 1:
                record.setUsername(param);
                wrapper.where("username={0}",param);
                break;
            case 2:
                record.setPhone(param);
                wrapper.where("phone={0}",param);
                break;
            case 3:
                record.setEmail(param);
                wrapper.where("email={0}",param);
                break;
            default:
                // 参数有误
                return null;
        }
        User user = super.selectOne(wrapper);
        return  user== null;
    }
    /*
     *  注册
     *
     * @author Tigger Wu
     * @date 2018/9/19 14:04
     * @param
     * @return
     */
    public Boolean doRegister(User user) {
        // 初始化的处理
        user.setId(null);
        user.setCreated(new Date());
        user.setUpdated(user.getCreated());
        // 加密处理，MD5加密
        user.setPassword(DigestUtils.md5Hex(user.getPassword()));
        return  super.insert(user) ==true;
    }
    /*
     *  登录操作
     * 
     * @author Tigger Wu
     * @date 2018/9/19 15:08
     * @param 
     * @return 
     */
    
    public String doLogin(String username, String password) throws JsonProcessingException {

        User record = new User();
        record.setUsername(username);
        EntityWrapper<User>wrapper = new EntityWrapper<>();
        wrapper.where("username={0}",username);
        User user = super.selectOne(wrapper);
        if (null == user) {
            // 用户不存在
            return null;
        }
        if (!StringUtils.equals(DigestUtils.md5Hex(password), user.getPassword())) {
            // 密码错误
            return null;
        }

        // 登录成功，将用户的信息保存到Redis中
        String token = DigestUtils.md5Hex(username + System.currentTimeMillis());

        this.redisService.set("TOKEN_" + token, MAPPER.writeValueAsString(user), REDIS_TIME);

        return token;
    }

    //从缓存中读取数据
    public User queryUserByToken(String token) {
        String key = "TOKEN_" + token;
        String jsonData = this.redisService.get(key);
        if (StringUtils.isEmpty(jsonData)) {
            // 登录超时
            return null;
        }

        //重新设置Redis中的生存时间
        this.redisService.expire(key, REDIS_TIME);

        try {
            return MAPPER.readValue(jsonData, User.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
